package main

import (
	"context"
	"fmt"
	"runtime"
	"time"
)

type myList struct {
	Data int
	Next *myList
}

func main() {
	data := []int{1, 2, 3, 10, 999, 8, 345, 7, 98, 33, 66, 77, 88, 68, 96}
	tm := time.NewTimer(time.Second * 5)
	ctx, cancel := context.WithCancel(context.Background())
	index := make(chan int)
	go catData(ctx, data, 88, index)
	select {
	case <-tm.C:
		fmt.Println("timeout....")
		cancel()
	case s := <-index:
		fmt.Println("get index:", s)
		cancel()
	}
	fmt.Println("main...........")

}
func catData(ctx context.Context, data []int, findData int, index chan int) {
	dataLen := len(data)
	count := (dataLen + 9) / 10
	for i := 0; i < count; i++ {
		go func(i int) {
			select {
			case <-ctx.Done():
				fmt.Println("time out .....sdfsdfsdfsdfsdf")
				return
			default:

				for j := i * 10; j < (i+1)*10; j++ {
					if j >= dataLen {
						fmt.Println("return")
						return
					}
					fmt.Println(data[j])
					if findData == data[j] {
						index <- j
						return
					}
				}
				return
			}
		}(i)
	}
}
func digui(head *myList) *myList {
	if head.Next == nil {
		return head
	}
	res := digui(head.Next)
	head.Next.Next = head
	head.Next = nil
	return res
}
func loop(data *myList) {
	next := data.Next
	dt := make([]int, 0)
	for next != nil {
		dt = append(dt, data.Data)
		data = next
		next = data.Next
	}
	fmt.Println(dt)
}
func f2() {
	var ch chan int
	go func() {
		ch <- 1
	}()
	//go func() {
	//	close(ch)
	//}()
	<-ch
	fmt.Println("main....")
}

func f1() {
	var ch chan int
	go func() {
		ch = make(chan int)
		ch <- 1
	}()
	go func(ch chan int) {
		time.Sleep(time.Second)
		<-ch

	}(ch)
	tk := time.Tick(time.Second)
	for range tk {
		fmt.Println("num:", runtime.NumGoroutine())
	}
}
